<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class EggController extends GlobalController
{
    public function download_code()
    {
        require_once APP_PATH . 'Admin/Util/phpqrcode/phpqrcode.php';
        $egg_id = I('get.egg_id', '', 'intval');
        if (!in_array($egg_id, range(1, 11))) {
            $egg_id = num_encode($egg_id);
        }
        $data = 'http://caidan.manyule.com/mobile?egg_id=' . $egg_id;
        $level = 'L';
        $size = 20;
        $rs = \QRcode::png($data, false, $level, $size, 1);
        header("Content-type: image/png");
        imagepng($rs);
        imagedestroy($rs);
    }

    public function getlist()
    {
        $pageSize = 20;
        $where = array();
        $eggModel = M('egg');
        $count = $eggModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $eggModel->where($where)->order('egg_id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $issueModel = M('comic_issue');
        $recordModel = M('smash_record');
        $commentModel = M('comment');
        foreach ($list as $key => $row) {
            $issueInfo = $issueModel->where(array('issue_id' => $row['issue_id']))->find();
            $list[$key]['issue_number'] = $issueInfo['issue_number'];

            $recordCount = $recordModel->where(array('egg_id' => $row['egg_id']))->count();
            $list[$key]['recordCount'] = $recordCount;

            $map = array('type' => 1, 'id' => $row['egg_id'], 'is_deleted' => '0');
            $commentCount = $commentModel->where($map)->count();
            $list[$key]['commentCount'] = $commentCount;
        }
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //添加或者修改
    public function save()
    {
        $eggModel = M('egg');
        if (IS_POST) {
            $egg_id = I('post.egg_id', 0, 'intval');
            if (I('post.auto_publish_status', 0, 'intval') == 1) {
                $date_str = I('post.auto_publish_time', '', 'trim') . ' ' . I('post.auto_publish_hour', '', 'trim');
                $data['auto_publish_time'] = strtotime($date_str);
            } else {
                $data['auto_publish_time'] = 0;
            }
            $data['issue_id'] = I('post.issue_id', 0, 'intval');
            $data['duration'] = I('post.duration', 0, 'intval');
            $data['egg_name'] = I('post.egg_name', '', 'trim');
            $data['egg_img'] = I('post.egg_img', '', 'trim');
            $data['share_img'] = I('post.share_img', '', 'trim');
            $data['hide_bottom_bar'] = I('post.hide_bottom_bar', 0, 'intval');
            $data['redirect_url'] = I('post.redirect_url', '', 'trim');
            $data['work_title'] = I('post.work_title', '', 'trim');
            $data['type'] = I('post.type', 'video', 'trim');
            $data['content'] = I('post.content', '', 'trim');
            $data['video_bg'] = I('post.video_bg', '', 'trim');
            $data['sound_bg'] = I('post.sound_bg', '', 'trim');
            $data['sound_img'] = I('post.sound_img', '', 'trim');
            if ($data['type'] != 'imgs') {
                $data['content'] = current(explode(',', $data['content']));
            }
            $data['description'] = I('post.description', '', 'trim');
            $data['description'] = str_replace("<p><br/></p>", '', $data['description']);
            if ($egg_id) {
                $eggModel->where(array('egg_id' => $egg_id))->save($data);
            } else {
                $row_number = $eggModel->count() + 1;
                $data['row_number'] = $row_number;
                $eggModel->add($data);
                $egg_id = $eggModel->getLastInsID();
            }
            $tplArr = C('TMPL_PARSE_STRING');
            //默认网页，则给一个简介页面
            if (I('post.type') == 'web' && empty($data['redirect_url'])) {
                $eggModel->where(array('egg_id' => $egg_id))->setField('redirect_url', $tplArr['__URL_ROOT__'] . '/mobile/description/egg_id/' . num_encode($egg_id));
            }
            $this->success('操作成功', array(array('返回列表', U('admin/egg/getlist'))));
            exit();
        }
        $egg_id = I('get.egg_id', 0, 'intval');
        $info = $eggModel->where(array('egg_id' => $egg_id))->find();
        if (!$info) {
            $issue_id = I('get.issue_id', 0, 'intval');
            $info = array('issue_id' => $issue_id, 'auto_publish_time' => 0);
        }
        $issueModel = M('comic_issue');
        $issueInfo = $issueModel->where(array('issue_id' => $info['issue_id']))->find();
        $list = $issueModel->order('issue_id desc')->limit(5)->select();
        $this->assign('list', $list);

        $this->assign('egg_id', $egg_id);
        $this->assign('auto_publish_hours', $this->auto_publish_hours);
        $this->assign('info', $info);
        $this->assign('issueInfo', $issueInfo);
        $this->display();
    }

    public function publish()
    {
        if (IS_POST) {
            $egg_id = I('post.egg_id', '', 'intval');
            $eggModel = M('egg');
            $eggModel->where(array('egg_id' => $egg_id))->setField('status', array('exp', '1 - status'));
            $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
        }
    }

    public function del()
    {
        $egg_id = I('post.egg_id', 0, 'intval');
        $eggModel = M('egg');
        $status = $eggModel->where(array('egg_id' => $egg_id))->getField('status');
        if ($status == 1) {
            $this->ajaxReturn('', '不能删除已发布的彩蛋', 'TIP_ERROR');
        }
        $bool = $eggModel->where(array('egg_id' => $egg_id))->delete();
        if ($bool) {
            $eggModel->where(array('egg_id' => array('gt', $egg_id)))->setDec('row_number', 1);
        }
        $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
    }
}